package com.qst.web;

import com.qst.po.JsonData;
import com.qst.po.PageData;
import com.qst.po.Schedule;
import com.qst.service.IScheduleService;
import com.qst.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;

@Controller
public class ScheduleController {
    @Autowired
    private IScheduleService scheduleService;
    @RequestMapping("toschedulelist")
    public String lists(){
        return "scheduleslist";
    }
    @RequestMapping("scheduleslists")
    @ResponseBody
    public PageData<Schedule> scheduleslists(int page, int limit){
        PageData<Schedule> data=new PageData<Schedule>();
        data.setData(scheduleService.getschedulesbypage(page, limit));
        data.setCode(0);
        data.setMsg("");
        data.setCount(scheduleService.getTotalCount());
        return data;
    }
    @RequestMapping("toaddschedule")
    public String toaddschedule(){
        return "addschedule";
    }

    @RequestMapping("addschedule")
    @ResponseBody
    public JsonData addschedule(int cid, int lid, String starttime, String endtime) throws ParseException {
        JsonData data =new JsonData();
        Schedule schedule =new Schedule();
        schedule.setCid(cid);
        schedule.setLid(lid);
        schedule.setStarttime(DateUtils.StringtoDate(starttime));
        schedule.setEndtime(DateUtils.StringtoDate(endtime));
        int count =scheduleService.addschedule(schedule);
        if(count>0){
            data.setCode(200);
        }else{
            data.setCode(500);
            data.setMsg("添加失败，请重新添加");
        }
        return data;
    }
    @RequestMapping("toupdateschedule")
    public String toupdateschedule(int id, HttpServletRequest request){
        Schedule schedule=scheduleService.getschedulesbyid(id);
        request.setAttribute("updateschedule",schedule);
        return "updateschedule";
    }
    @RequestMapping("updateschedule")
    @ResponseBody
    public JsonData updateschedule(int id,int cid,int lid,String starttime,String endtime) throws ParseException {
        JsonData data =new JsonData();
        Schedule schedule =new Schedule();
        schedule.setId(id);
        schedule.setCid(cid);
        schedule.setLid(lid);
        schedule.setStarttime(DateUtils.StringtoDate(starttime));
        schedule.setEndtime(DateUtils.StringtoDate(endtime));
        int count =scheduleService.updateschedule(schedule);
        if(count>0){
            data.setCode(200);
        }else{
            data.setCode(500);
            data.setMsg("修改失败，请重新修改");
        }
        return data;
    }
    @RequestMapping("deleteschedule")
    @ResponseBody
    public JsonData deleteschedule(int id){
        JsonData data =new JsonData();
        int count =scheduleService.deleteschedule(id);
        if(count>0){
            data.setCode(200);
        }else{
            data.setCode(500);
            data.setMsg("删除失败，请重新删除");
        }
        return data;
    }
}
